/**
 * ListController
 *
 * @description :: Server-side logic for managing lists
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */

var apiLib = require('./MainApi');
var Promise = require('promise');
module.exports = {

  index: function (req, res) {
    List.find().then(function (allCategory) {
      Product.find().then(function (allProducts) {
        res.view('list', {categories: allCategory, products: allProducts});
      }).catch(function (err) {
        res.serverError(err)
      })
      }).catch(function(err){
        res.serverError(err)
      }
    )
  },

  all: function (req, res) {
    List.find().then(function (allCategory) {
      res.send(allCategory);
    }).catch(function (err) {
      res.serverError(err);
    })
  },

  cat: function (req, res) {
    var catId = req.params.id;
    if (catId == 'all') {
      Product.find().then(function (allProducts) {
        res.send(allProducts);
      }).catch(function (err) {
        res.serverError(err);
      })
    } else {
      List.find({id: catId}).then(function (catName) {
        Product.find({category: catName[0].name}).then(function (catProduct) {
          res.send(catProduct);
        }).catch(function (err) {
          res.serverError(err);
        })
      }).catch(function (err) {
        res.serverError(err);
      })
    }

  },

  detail: function (req, res) {
    var productId = req.params.id;
    Product.find({sn: productId}).then(function (record) {
      res.view('detail', {layout: 'foo',data: record});
    }).catch(function (err) {
      res.serverError(err);
    })
  },

  getCat: function (req, res) {
    apiLib.getAllCats(function (call) {
      res.send(call);
    })
  },

  getBrandsByCat: function (req, res) {
    var category = req.param('category');
    console.log(category);
    apiLib.getBrandsByCat(category, function (call) {
      res.send(call);
    })
  },

  getProductsByBrand: function (req, res) {
    var brand = req.param('brand');
    apiLib.getProductsByBrand(brand, function (call) {
      res.send(call);
    })
  },

  getProductInfo: function (req, res) {
    if (req.param('sn')) {
      var sn = req.param('sn');
      console.log('sn is:',sn);
      Classify.find({parent_sn: sn, parent_id: 1}).then(function (records) {
        if (records.length == 0) {
          res.send(records);
        } else {
          console.log(records);
          var c_sn = JSON.parse(records[0].child_sn);
          console.log('c_sn:', c_sn);
          var p_info = [];
          var get_products_info = new Promise(function (resolve, reject) {
            for(var i=0; i<c_sn.length; i++){
              var get_product_info = new Promise(function (resolve, reject) {
                apiLib.getProductBySn(c_sn[i], function (call) {
                  console.log('product info:',call);
                  if (call.length > 0) {
                    resolve(JSON.parse(call)[0]);
                  }
                })
              });
              Promise.all([get_product_info]).then(function (result) {
                p_info.push(result[0]);
                if (p_info.length == c_sn.length) {
                  resolve(p_info);
                }
              });
            }
          });
          Promise.all([get_products_info]).then(function (results) {
            res.send({products: results[0]});
          })
        }
      })
      }
  },
  // category: function (req, res) {
  //   if (req.params.id) {
  //     var qid = req.params.id;
  //     List.find({id: qid}).then(function (catName) {
  //       Product.find({category: catName}).then(function (result) {
  //         res.view('list', {data: result});
  //       }).catch(function (err) {
  //         res.serverError(err)
  //       })
  //     }).catch(function (err) {
  //       res.serverError(err)
  //     })
  //   }
  // },

};

